package com.fire.factory;

import com.fire.controller.IStudentController;
import com.fire.controller.ITeacherController;
import com.fire.controller.impl.StudentControllerImpl;
import com.fire.dao.IStudentDao;
import com.fire.dao.ITeacherDao;
import com.fire.dao.impl.StudentDaoImpl;
import com.fire.domain.Student;
import com.fire.service.IStudentService;
import com.fire.service.ITeacherService;
import com.fire.service.impl.StudentServiceImpl;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import static java.lang.Class.forName;

public class Factory {

    private static Properties prop = new Properties();
    private static HashMap<String, Object> map = new HashMap<>();

    static {
        try {
            prop.load(ClassLoader.getSystemResourceAsStream("prop.properties"));

            prop.forEach((k, v) -> {
                try {
                    Object value = prop.getProperty(k.toString());
                    Class<?> clazz = forName(value.toString());
                    Constructor<?> constructor = clazz.getDeclaredConstructor();
                    constructor.setAccessible(true);
                    Object o = constructor.newInstance();
                    map.put(k.toString(), o);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static ITeacherService getTeacherServiceImpl() {

        if (map.get("TeacherServiceImpl")==null){
            try {
                Class<?> clazz = forName("com.fire.service.impl.TeacherServiceImpl");
                Constructor<?> constructor = clazz.getDeclaredConstructor();
                constructor.setAccessible(true);
                Object o = constructor.newInstance();
                map.put("TeacherServiceImpl", o);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return (ITeacherService) map.get("TeacherServiceImpl");
    }

    public static IStudentController getStudentControllerImpl() {

        if (map.get("StudentControllerImpl")==null){
            try {
                Class<?> clazz = forName("com.fire.controller.impl.StudentControllerImpl");
                Constructor<?> constructor = clazz.getDeclaredConstructor();
                constructor.setAccessible(true);
                Object o = constructor.newInstance();
                map.put("StudentControllerImpl", o);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return (IStudentController) map.get("StudentControllerImpl");
    }

    public static ITeacherController getTeacherControllerImpl() {

        if (map.get("TeacherControllerImpl")==null){
            try {
                Class<?> clazz = forName("com.fire.controller.impl.TeacherControllerImpl");
                Constructor<?> constructor = clazz.getDeclaredConstructor();
                constructor.setAccessible(true);
                Object o = constructor.newInstance();
                map.put("TeacherControllerImpl", o);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return (ITeacherController) map.get("TeacherControllerImpl");
    }

    public static ITeacherDao getTeacherDaoImpl() {

        if (map.get("TeacherDaoImpl")==null){
            try {
                Class<?> clazz = forName("com.fire.dao.impl.TeacherDaoImpl");
                Constructor<?> constructor = clazz.getDeclaredConstructor();
                constructor.setAccessible(true);
                Object o = constructor.newInstance();
                map.put("TeacherDaoImpl", o);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        return (ITeacherDao) map.get("TeacherDaoImpl");
    }

    public static IStudentDao getStudentDaoImpl() {

        if (map.get("StudentDaoImpl")==null){
            try {
                Class<?> clazz = forName("com.fire.dao.impl.StudentDaoImpl");
                Constructor<?> constructor = clazz.getDeclaredConstructor();
                constructor.setAccessible(true);
                Object o = constructor.newInstance();
                map.put("StudentDaoImpl", o);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        return (IStudentDao)map.get("StudentDaoImpl");
    }

    public static IStudentService getStudentServiceImpl() {
        if (map.get("StudentServiceImpl")==null){
            try {
                Class<?> clazz = forName("com.fire.service.impl.StudentServiceImpl");
                Constructor<?> constructor = clazz.getDeclaredConstructor();
                constructor.setAccessible(true);
                Object o = constructor.newInstance();
                map.put("StudentServiceImpl", o);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return (IStudentService)map.get("StudentServiceImpl");
    }
}
